package com.pocket.demo.core.listener;

import com.pocket.demo.core.initializing.PocketInitialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p> ApplicationListener 可选监听事件：
 *      ContextRefreshedEvent：ApplicationContext 被初始化或刷新时。</br>
 *          此时：所有的Bean被成功装载，后处理Bean被检测并激活，所有Singleton Bean 被预实例化。</br>
 *      ContextStartedEvent：</br>
 *      ContextStoppedEvent：</br>
 *      ContextClosedEvent：</br>
 *      RequestHandledEvent：</br>
 *      ServletWebServerInitializedEvent：</br>
 *      ServletRequestHandledEvent：</br>
 *      ServletRequestEvent：</br>
 * @author zhaozhile
 */
@Component
public class InitListener implements ApplicationListener<ApplicationReadyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(InitListener.class);


    @Autowired
    private List<PocketInitialize> pocketInitializers;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        if (pocketInitializers != null && pocketInitializers.size() > 0){
            pocketInitializers.forEach( item -> {
                logger.debug("Pocket ApplicationReadyEvent init class ：{}", item.getClass().getName());
                item.initialize(event);
            });
        }
    }
}
